<template>
  <el-dialog v-model="visible" :title="!dataForm.purchaseProjectId ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="项目名称" prop="purchaseProjectName">
            <el-input v-model="dataForm.purchaseProjectName" placeholder="项目名称"></el-input>
          </el-form-item>
          <el-form-item label="项目编号" prop="purchaseProjectCode">
            <el-input v-model="dataForm.purchaseProjectCode" placeholder="项目编号"></el-input>
          </el-form-item>
          <el-form-item label="交易编号" prop="tradeCode">
            <el-input v-model="dataForm.tradeCode" placeholder="交易编号"></el-input>
          </el-form-item>
          <el-form-item prop="purchaseProjectType" label="项目类型">
            <ren-radio-group v-model="dataForm.purchaseProjectType" dict-type="purchase_project_type"></ren-radio-group>
          </el-form-item>
          <el-form-item prop="purchaseMode" label="采购方式">
            <ren-radio-group v-model="dataForm.purchaseMode" dict-type="purchase_mode"></ren-radio-group>
          </el-form-item>
          <el-form-item label="采购组织形式" prop="tenderOrganizeForm">
            <ren-radio-group v-model="dataForm.tenderOrganizeForm" dict-type="tender_organize_form"></ren-radio-group>
          </el-form-item>
          <el-form-item label="采购人共享平台" prop="purchaseShareId">
            <ren-dept-tree v-model="dataForm.purchaseShareId" placeholder="请选择" v-model:deptName="dataForm.purchaseShareName"></ren-dept-tree>
          </el-form-item>
<!--          <el-form-item label="采购人名称" prop="purchaseBodyName">-->
<!--            <el-input v-model="dataForm.purchaseBodyName" placeholder="采购人名称"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="代理机构共享平台" prop="agencyShareId">
            <ren-dept-tree v-model="dataForm.agencyShareId" placeholder="请选择" v-model:deptName="dataForm.agencyBodyName"></ren-dept-tree>
          </el-form-item>
<!--              <el-form-item label="代理机构名称" prop="agencyBodyName">-->
<!--            <el-input v-model="dataForm.agencyBodyName" placeholder="代理机构名称"></el-input>-->
<!--          </el-form-item>-->
              <el-form-item label="采购内容" prop="projectContent">
            <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" v-model="dataForm.projectContent" placeholder="采购内容"></el-input>
          </el-form-item>
              <el-form-item label="项目资金来源" prop="fundSource">
                <el-input v-model="dataForm.fundSource" placeholder=""></el-input>
          </el-form-item>
              <el-form-item label="项目概算金额(元)" prop="estimatesMoney">
                <el-input  v-model="dataForm.estimatesMoney" :min="0" type="number" :step="1" placeholder="请输入" />
<!--                <el-input v-model="dataForm.estimatesMoney" placeholder="项目概算金额(元)"></el-input>-->
          </el-form-item>
              <el-form-item label="项目联系人" prop="linkMan">
            <el-input v-model="dataForm.linkMan" placeholder="项目联系人"></el-input>
          </el-form-item>
          <el-form-item label="联系人手机号码" prop="linkMobile">
            <el-input v-model="dataForm.linkMobile" placeholder="联系人手机号码"></el-input>
          </el-form-item>
<!--          <el-form-item label="当前环节" prop="processStatus">-->
<!--                <ren-radio-group v-model="dataForm.processStatus" dict-type="purchase_process_status"></ren-radio-group>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="当前环节名称" prop="processName">-->
<!--            <el-input v-model="dataForm.processName" placeholder="当前环节名称"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="创建主体共享平台" prop="createBodyShareId">
            <ren-dept-tree v-model="dataForm.createBodyShareId" placeholder="请选择" v-model:deptName="dataForm.createBodyShareName"></ren-dept-tree>
          </el-form-item>
<!--           <el-form-item label="公告是否发布" prop="isPublishAnnouncement">-->
<!--             <ren-radio-group v-model="dataForm.isPublishAnnouncement" dict-type="is_or_no"></ren-radio-group>-->
<!--          </el-form-item>-->
<!--              <el-form-item label="项目创建人" prop="createUser">-->
<!--            <el-input v-model="dataForm.createUser" placeholder="项目创建人"></el-input>-->
<!--          </el-form-item>-->
<!--              <el-form-item label="项目创建时间，格式：YYYY-MM-DD HH:MM:SS" prop="createTime">-->
<!--            <el-input v-model="dataForm.createTime" placeholder="项目创建时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--          </el-form-item>-->
<!--              <el-form-item label="最后更新人" prop="updateUser">-->
<!--            <el-input v-model="dataForm.updateUser" placeholder="最后更新人"></el-input>-->
<!--          </el-form-item>-->
<!--              <el-form-item label="最后更新时间，格式：YYYY-MM-DD HH:MM:SS" prop="updateTime">-->
<!--            <el-input v-model="dataForm.updateTime" placeholder="最后更新时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--          </el-form-item>-->
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { isEmail, isMobile } from "@/utils/utils";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  purchaseProjectId: '',  purchaseProjectName: '',  purchaseProjectCode: '',  tradeCode: '',  purchaseProjectType: '',  purchaseMode: '',  tenderOrganizeForm: '',  purchaseShareId: '',  purchaseBodyName: '',  agencyShareId: '',  agencyBodyName: '',  projectContent: '',  fundSource: '',  estimatesMoney: '',  linkMan: '',  linkMobile: '',  processStatus: '',  processName: '',  createBodyShareId: '',  isPublishAnnouncement: '',  createUser: '',  createTime: '',  updateUser: '',  updateTime: ''});


const validateMobile = (rule: any, value: string, callback: (e?: Error) => any): any => {
  if (value && !isMobile(value)) {
    return callback(new Error("手机格式错误"));
  }
  callback();
};

const rules = ref({
          purchaseProjectName: [
      { required: true, message: '项目名称不能为空', trigger: 'blur' }
    ],
          purchaseProjectCode: [
      { required: true, message: '项目编号不能为空', trigger: 'blur' }
    ],
          purchaseProjectType: [
      { required: true, message: '项目类型不能为空', trigger: 'blur' }
    ],
          purchaseMode: [
      { required: true, message: '采购方式不能为空', trigger: 'blur' }
    ],
          tenderOrganizeForm: [
      { required: true, message: '采购组织形式不能为空', trigger: 'blur' }
    ],
          projectContent: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          estimatesMoney: [
      { required: true, message: '必填项不能为空', trigger: 'blur' },
    {
      trigger: "blur",
      pattern: /^[0-9]+(.[0-9]{1,2})?$/, //大于0保留两位
      message: "输入的值必须为正数且保留两位小数"
    },
    {
      trigger: "blur",
      pattern: /^\d{1,9}$|^\d{1,9}[.]\d{1,2}$/, //大于0保留两位
      message: "输入的值最大为999999999.99且最多两位小数"
    }
    ],
          linkMan: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          linkMobile: [
      { required: true, message: '必填项不能为空', trigger: 'blur' },
      { validator: validateMobile, trigger: "blur" }
    ]
  });



const init = (purchaseProjectId?: number) => {
  visible.value = true;
  dataForm.purchaseProjectId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (purchaseProjectId) {
    getInfo(purchaseProjectId);
  }
};

// 获取信息
const getInfo = (purchaseProjectId: number) => {
  baseService.get("/purchaseproject/purchaseproject/" + purchaseProjectId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.purchaseProjectId ? baseService.post : baseService.put)("/purchaseproject/purchaseproject", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
